package com.parent.elasticsearch.old;


import com.parent.elasticsearch.old.es.Shards;
import com.parent.elasticsearch.old.esentitys.Hits1;
import com.parent.elasticsearch.old.esentitys.HitsNew1;
import com.parent.elasticsearch.old.result.Hits;
import com.parent.elasticsearch.old.result.HitsNew;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author yan
 * @Date 2023/7/19 0019 13:45
 * @Description
 */

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ElasticsearchR<T> implements Serializable {


    private static final long serialVersionUID = 5654643600557170372L;
    private Integer took;
    /**
     * 是否超时
     */
    private Boolean timed_out;
    private Shards _shards;
    private Hits1<T> hits;




    /**
     *
     * @param elasticsearchR
     * @return
     * @param <T>
     */
    public static <T> ElasticsearchResult<T> toResult(ElasticsearchR<T> elasticsearchR) {


        ElasticsearchResult<T> elasticsearchResult = new ElasticsearchResult<>();
        BeanUtils.copyProperties(elasticsearchR, elasticsearchResult);

        Boolean timed_out = elasticsearchR.getTimed_out();
        Shards shards = elasticsearchR.get_shards();
        elasticsearchResult.setTimedOut(timed_out);
        elasticsearchResult.setShards(shards);

        Hits1 hits = elasticsearchR.getHits();

        Hits hitsResult = Hits1.toResult(hits);

        List<HitsNew1<T>> hits1 = hits.getHits();

        List<HitsNew> hitsNewsResult = hits1.stream().map(v -> {
            HitsNew hitsNew = HitsNew1.toResult(v);
            return hitsNew;
        }).collect(Collectors.toList());
        hitsResult.setHits(hitsNewsResult);
        elasticsearchResult.setHits(hitsResult);


        return  elasticsearchResult;
    }
}
